<template>
  <div v-if="SongDetail">
    <!-- 全屏播放器 -->
    <div class="player" v-show="showPlayer">
      <div
        class="bg"
        :style="'background-image:url(' + SongDetail.al.picUrl + ')'"
      ></div>
      <div class="shade"></div>
      <div class="view">
        <div class="button">
          <span
            class="iconfont icon-xiajiantou"
            @click.stop="showPlayerTrue"
          ></span>
        </div>
        <div class="title">
          <h2>{{ SongDetail.name }}</h2>
          <h3>
            <span v-for="(item, index) in SongDetail.ar" :key="index">
              {{ item.name }}
            </span>
          </h3>
        </div>
        <div class="content">
          <div
            v-show="togglelyric"
            @click="togglelyric = false"
            :class="['img', 'play', { pause: iconplay }]"
          >
            <img :src="SongDetail.al.picUrl" alt="" />
          </div>
          <div class="lyrics" v-show="!togglelyric" @click="togglelyric = true">
            <ul :style="{ top: lyricTop }">
              <li
                v-for="(item, index) in lyric"
                :class="{ con: currentRow == index }"
                :key="index"
              >
                {{ item.text }}
              </li>
            </ul>
          </div>
        </div>

        <!-- 滑块 -->
        <div class="range">
          <div class="bar">
            <span class="timeLeft">{{ timeLeft | playerTime }}</span>
            <span class="timeRight">{{ timeRight | playerTime }}</span>
            <van-slider
              @change="sliderchange"
              v-model="value"
              active-color="#ee0a24"
              bar-height="4px"
              inactive-color="#514E4c"
            >
              <template #button>
                <div class="custom-button"></div>
              </template>
            </van-slider>
          </div>
        </div>

        <div class="buttons">
          <div
            v-if="playerType == 1"
            @click="changeType"
            class="iconfont icon-24gl-repeat2"
          ></div>
          <div
            v-else
            @click="changeType"
            class="iconfont icon-24gl-shuffle"
          ></div>
          <div
            class="iconfont icon-shangyishoushangyige"
            @click="startup"
          ></div>
          <div
            :class="['iconfont', iconplay ? 'icon-bofang' : 'icon-zanting1']"
            @click.stop="toggleplay"
          ></div>
          <div class="iconfont icon-xiayigexiayishou" @click="lowerFun"></div>
          <div
            :class="['iconfont', 'icon-aixin', { changeRed: likeType == 1 }]"
            @click="likemusic"
          ></div>
          <div class="iconfont icon-pinglun" @click="showPopup"></div>
          <div class="iconfont icon-xitongxiazaibendi"></div>
        </div>
      </div>
    </div>

    <!-- 评论 -->
    <van-popup
      v-model="show"
      position="bottom"
      closeable
      :style="{ height: '60%' }"
      class="review-box"
    >
      <van-tabs  v-model="activeName" @click="commentHot">
          

        <van-tab title="最新评论" name="a">
          <div class="review">
            <div class="text">评论区</div>
            <div class="item" v-for="item in comments" :key="item.commentId">
              <div class="img">
                <van-image lazy-load :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content">
                <div class="name">{{ item.user.nickname }}</div>
                <div class="time">{{ changeTime(item.time) }}</div>
                <div class="content-text">{{ item.content }}</div>
                
                <div class="praise">
                  {{ item.likedCount }}
                  <span :class="['iconfont ','icon-dianzan',{changePraise:item.liked}]" @click="praiseFun(item)"></span>
                </div>
              </div>
            </div>
          </div>
        </van-tab>
        <van-tab title="热门评论" name="b" >
            <div class="review">
            <div class="text">评论区</div>
            <div class="item" v-for="item in hotComments" :key="item.commentId">
              <div class="img">
                <van-image lazy-load :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content">
                <div class="name">{{ item.user.nickname }}</div>
                <div class="time">{{ changeTime(item.time) }}</div>
                <div class="content-text">{{ item.content }}</div>
               
                <div class="praise">
                  {{ item.likedCount }}
                  <span :class="['iconfont ','icon-dianzan',{changePraise:item.liked}]" @click="praiseFun(item)"></span>
                </div>
              </div>
            </div>
          </div>
        </van-tab>
      </van-tabs>

      <div class="review-ipt">
          <input class="ipt-box" v-model="myreview" placeholder="也许这次是你上热门" type="text">
          <div class="btn" @click="reviewFun">发送</div>
      </div>
    </van-popup>

    <!-- 迷你播放器 -->
    <div class="mini-player">
      <div
        :class="['img', 'play', { pause: iconplay }]"
        @click="showPlayer = true"
      >
        <img :src="SongDetail.al.picUrl" alt="" />
      </div>

      <div class="text">
        <h2>{{ SongDetail.name }}</h2>
        <h3>
          <span v-for="(item, index) in SongDetail.ar" :key="index">
            {{ item.name }}
          </span>
        </h3>
      </div>

      <div class="control" @click.stop="toggleplay">
        <span class="iconfont icon-bofang" v-show="iconplay"></span>
        <span class="iconfont icon-zanting1" v-show="!iconplay"></span>
      </div>
      <span class="iconfont icon-caidan"></span>

    </div>
    <!-- 音频播放器 -->
    <audio
      hidden
      ref="audio"
      controls
      :src="musicData"
      @loadeddata="loadeddataFun"
      @ended="endedFun"
    ></audio>
  </div>
</template>

<script>
import {
  getSongUrl,
  getSongDetail,
  getLyric,
  getLike, //收藏
  getCommentNew,// 最新评论
  getCommentHot, //最热评论
  getComment,  //发送评论
  getCommentLike , //点赞
} from "../../api/base";
import { getPlaylistDetail } from "../../api/rank";
import { Toast } from "vant";

export default {
  props: ["playerId", "listId"],
  data() {
    return {
      
      myreview:"", //我的评论
      activeName: "a",
      show: false,
      musicData: "",
      iconplay: true, //播放按钮
      SongDetail: null, //歌曲信息
      value: 0, //滚动条0到100之间
      showPlayer: false,
      timeRight: 0, //右侧时间
      timeLeft: 0, //左侧时间
      clearset: null, //定时器
      playerLists: [], //播放列表
      togglelyric: true, //歌词切换
      lyric: [], //歌词数组
      lyricTop: "200px", //歌词位置
      currentRow: 0, //歌词高亮
      playerType: 1, // 播放类型 1.列表播放 2.随机播放
      likeType: 2, //1.收藏歌曲 , 2.取消收藏
      comments: [], //最新歌曲评论
      hotComments:[],// 热门歌曲评论
     
    };
  },
  methods: {
    // 点击评论
    showPopup() {
      this.show = true;
      // 调用最新评论接口
      getCommentNew({ id: this.playerId, type: 0, sortType: 3 }).then(
        (data) => {
         
          this.comments = data.data.comments;
        }
      );
    },
   
      // 展示热门评论
    commentHot(){
      console.log(111);
        getCommentHot({id:this.playerId,type:0}).then(data=>{
           console.log(data.hotComments);
           this.hotComments = data.hotComments
        })
    },

    // 发表评论
    reviewFun(){
        getComment({t:1,type:0,id:this.playerId,content:this.myreview}).then(()=>{})
        // 清空输入框
        this.myreview = ""
    },

    // 点赞
    praiseFun(item){
       
       item.liked= !item.liked
       item.likedCount = item.liked==true ? item.likedCount+1 : item.likedCount-1
       getCommentLike({id:this.playerId,cid:item.commentId,t:1,type:0}).then(()=>{})
    },

    // 把时间戳转为正常格式
    changeTime(time) {
    
      return new Date(time).toLocaleDateString();
    },

    // 获取歌曲url
    getSongUrlFun() {
      getSongUrl({ id: this.playerId }).then((data) => {
        this.musicData = data.data[0].url;
        this.iconplay = true;
      });
    },

    // 获取歌曲信息
    getSongDetailFun() {
      getSongDetail({ ids: this.playerId }).then((data) => {
        this.SongDetail = data.songs[0];
      });
    },
    // 获取歌曲的歌词
    getLyricFun() {
      getLyric({ id: this.playerId }).then((data) => {
        // console.log(data);
        // console.log(data.lrc.lyric);
        this.formatLyric(data.lrc.lyric);
      });
    },

    // 处理歌词结构
    formatLyric(text) {
      this.lyric = [];
      let arr = text.split("\n"); //把原歌词切割成数组
      let row = arr.length;
      for (let i = 0; i < row; i++) {
        let temp_row = arr[i];
        let temp_arr = temp_row.split("]");
        let lyrictext = temp_arr.pop(); //删除数组最后一个，返回值
        temp_arr.forEach((ele) => {
          let obj = {};
          //  切割时间
          let time_arr = ele.substr(1, ele.length - 1).split(":");
          let s = time_arr[0] * 60 + Math.ceil(time_arr[1]); //分钟变为秒
          obj.text = lyrictext;
          obj.time = s;
          if (lyrictext.length > 0) {
            this.lyric.push(obj); // 每一行歌词处理后放入数组中
          }
        });
      }
      //  console.log(arr);
    },

    // 播放暂停音乐
    toggleplay() {
      this.$emit("animationFun",this.iconplay)
      if (this.iconplay) {
        this.$refs.audio.play(); //播放音乐

        clearInterval(this.clearset); //清除定时器
        this.clearset = setInterval(() => {
          let currentTime = this.$refs.audio.currentTime; //实时变化时间
          let duration = this.$refs.audio.duration; //总时间
          this.timeLeft = currentTime;
          // 计算滚动条值
          this.value = (currentTime / duration) * 100;
          if (this.value == 100) {
            clearInterval(this.clearset);
            this.$refs.audio.pause(); //暂停音乐
            this.iconplay = true;
          }
        }, 999);
      } else {
        this.$refs.audio.pause(); //暂停音乐
        clearInterval(this.clearset);
      }
      this.iconplay = !this.iconplay; //切换页面状态
    },

    // 音乐加载完成后 获取音乐数据
    loadeddataFun() {
      this.toggleplay(); // 自动播放功能
      let duration = this.$refs.audio.duration;
      this.timeRight = duration;
    },
    // 音乐播放结束后
    endedFun() {
      this.iconplay = true;
      // 根据播放类型 选择下一首播放
      if (this.playerType == 1) {
        // 列表播放
        this.lowerFun(); //下一首歌
      } else {
        // 随机播放
        let index = parseInt(Math.random() * this.playerLists.length);
        
        this.$emit("set-player-id", this.playerLists[index].id);
      }
    },

    // 获取滚动条变化
    sliderchange() {
      let duration = (this.$refs.audio.duration * this.value) / 100;
      this.timeLeft = duration;
      this.$refs.audio.currentTime = duration; //拉动滚动条修改时间
    },
    // 下一首歌
    lowerFun() {
      if (this.playerType == 1) {
        // 列表播放
        let id = this.SongDetail.id; //获取id
        // 获取在播放歌曲列表中的下标
        let index = this.playerLists.findIndex((item) => item.id == id);
        // 1.获取当前歌曲下标 下标加1
        index = index + 1 > this.playerLists.length - 1 ? 0 : index + 1;
        //  返回父组件歌曲id
        this.$emit("set-player-id", this.playerLists[index].id);
      } else {
        // 随机播放
        let index = parseInt(Math.random() * this.playerLists.length);
        
        this.$emit("set-player-id", this.playerLists[index].id);
      }
    },
    // 上一首歌
    startup() {
      if (this.playerType == 1) {
        // 列表播放
        let id = this.SongDetail.id; //获取id
        // 获取在播放歌曲列表中的下标
        let index = this.playerLists.findIndex((item) => item.id == id);
        // 1.获取当前歌曲下标 下标减1
        index = index - 1 < 0 ? this.playerLists.length - 1 : index - 1;
        //  返回父组件歌曲id
        this.$emit("set-player-id", this.playerLists[index].id);
      } else {
        // 随机播放
        let index = parseInt(Math.random() * this.playerLists.length);
        
        this.$emit("set-player-id", this.playerLists[index].id);
      }
    },

    // 播放类型切换
    changeType() {
      this.playerType = this.playerType == 1 ? 2 : 1;
      if (this.playerType == 1) {
        Toast("列表循环");
      } else {
        Toast("随机播放");
      }
    },

    // 收藏与取消收藏
    likemusic() {
      this.likeType = this.likeType == 1 ? 2 : 1;
    },

    // 播放器下拉
    showPlayerTrue() {
      this.showPlayer = false;

      // 播放器下拉的是时候 请求收藏音乐接口
      let type = this.likeType == 1 ? true : false;
      getLike({ id: this.playerId, like: type }).then((data) => {
        console.log(data.code);
      });

      
       
    },
  },

  // 只要数据更改就重新更新
  watch: {
    //   侦听播放id
    playerId() {
      this.getSongUrlFun(); //歌曲的url
      this.getSongDetailFun(); //歌曲详情url
      this.getLyricFun(); //歌词的url
    },
    // 侦听播放列表id
    listId() {
      console.log("歌单数据");
      getPlaylistDetail({ id: this.listId }).then((data) => {
        this.playerLists = data.playlist.tracks; //获取歌单数据
        // console.log(this.playerLists);
      });
    },

    // 左侧歌词时间变化
    timeLeft() {
      for (let i = 0; i < this.lyric.length; i++) {
        if (parseInt(this.timeLeft) <= this.lyric[i].time) {
          this.lyricTop = 200 - i * 36 + "px";
          this.currentRow = i; // 歌词高亮
          break;
        }
      }
    },
  },

  filters: {
    playerTime(data) {
      let m = parseInt(data / 60); //分钟
      let s = parseInt(data % 60); // 秒
      if (m == 0 && s == 0) {
        return "0:00";
      } else {
        return m + ":" + (s < 10 ? "0" + s : s);
      }
    },
  },
};
</script>

<style lang="less">
.player {
  position: fixed;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 1);
  color: #fff;
  z-index: 2;
  .bg {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    // background-image: url("http://p3.music.126.net/lFFiY-jvO1ENHtK2bZ6eUw==/109951166531420475.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    background-position: 50%;
    transform: scale(1.5);
    transform-origin: center center;
    filter: blur(30px);
    z-index: 1;
  }
  .shade {
    position: absolute;
    z-index: 3;
    left: 0px;
    bottom: 0px;
    height: 50%;
    width: 100%;
    background-image: linear-gradient(180deg, rgba(0, 0, 0, 0), #000);
  }
  .view {
    position: absolute;
    z-index: 4;
    left: 0px;
    bottom: 0px;
    height: 100%;
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
    .button {
      font-size: 22px;
      position: absolute;
      left: 13px;
      top: 13px;
      font-weight: bold;
    }
    .title {
      text-align: center;
      h2 {
        font-size: 20px;
      }
      h3 {
        font-size: 14px;
      }
    }
    .content {
      text-align: center;
      height: 65%;
      width: 100%;
      position: relative;
      .img {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        margin: auto;
        width: 300px;
        height: 300px;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.3);

        img {
          width: 274px;
          height: 274px;
          border-radius: 50%;
          background: rgba(0, 0, 0, 0.3);
          padding: 13px;
        }
        &.play {
          animation: rotate 20s linear infinite;
        }
        &.pause {
          animation-play-state: paused;
        }
      }
      .lyrics {
        width: 100%;
        height: 100%;
        position: relative;
        overflow: hidden;
        ul {
          position: absolute;
          width: 100%;
          top: 180px;
          left: 0px;
          transition: all 0.3s linear;
          li {
            height: 36px;
            line-height: 36px;
            color: #cbcccc;
            font-size: 16px;
            &.con {
              color: #fff;
            }
          }
        }
      }
    }
  }

  .range {
    position: absolute;
    width: 100%;
    left: 0;
    bottom: 110px;
    text-align: center;
    .bar {
      width: 66%;
      margin: auto;
      position: relative;
      font-size: 14px;
      .timeLeft {
        position: absolute;
        left: -35px;
        top: -6px;
      }
      .timeRight {
        position: absolute;
        right: -35px;
        top: -6px;
      }
    }
  }
  .buttons {
    display: flex;
    position: absolute;
    bottom: 50px;
    width: 100%;
    line-height: 25px;

    .iconfont {
      flex: 1;
      width: 20%;
      text-align: center;
      font-size: 24px;
    }
    .icon-bofang,.icon-zanting1{
      font-size: 40px;
    }
    .changeRed {
      color: red;
    }
    .icon-pinglun {
      position: absolute;
      top: -80px;
      left: 240px;
    }
    .icon-xitongxiazaibendi{
      position: absolute;
      top: -80px;
      left: 60px;
    }
  }
}
.custom-button {
  width: 6px;
  height: 6px;
  background-color: #d0261a;
  border-radius: 50%;
  border: 5px solid #fff;
}

.mini-player {
  position: fixed;
  left: 0px;
  bottom: 0px;
  width: 100%;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.9);
  display: flex;
  .img {
    width: 37px;
    height: 37px;
    border-radius: 50%;
    margin: 7px 9px 0px 17px;
    background-color: aqua;
    img {
      width: 37px;
      height: 37px;
      border-radius: 50%;
    }
    &.play {
      animation: rotate 20s linear infinite;
    }
    &.pause {
      animation-play-state: paused;
    }
  }
  .text {
    flex: 1;
    h2 {
      font-size: 16px;
      line-height: 16px;
      color: #181819;
      margin-top: 12px;
    }
    h3 {
      font-size: 13px;
      line-height: 13px;
      color: #686a6a;
    }
  }
  .control {
    margin-top: 11px;

    .iconfont {
      font-size: 30px;
      margin-right: 12px;
      color: #727373;
    }
    .icon-zanting {
      font-size: 24px;
      text-align: center;
      
    }
  }
  .icon-caidan{
    display: block;
   font-size: 32px;
    margin-top: 8px;
      color: #727373;

  }
}

// 评论
.review-box{
  position: relative;
  .review-ipt{
   
    width: 100%;
    height: 60px;
    background-color: rgba(255, 255, 255, 0.9);
    position: fixed;
    bottom: 0px;
    left: 0px;
    display: flex;
    align-items: center;
    .ipt-box{
     height: 25px;
      border: 0px;
      padding: 0px;
      margin-left: 30px;
      font-size: 14px;
      flex: 1;
    }

    .btn{
      width: 40px;
      height: 25px;
      line-height: 25px;
      font-size: 14px;
      
    }
  }
  .review {
  width: 100%;
  // height: 600px;
  padding-left: 10px;
  padding-top: 30px;
  box-sizing: border-box;
  .text {
    margin-bottom: 10px;
  }
  .item {
    display: flex;
    margin-bottom: 20px;
    .img {
      width: 40px;
      height: 40px;
      background-color: red;
      margin-right: 8px;
      border-radius: 50%;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
      }
    }
    .content {
      flex: 1;
      border-bottom: 1px solid #ced5d5;
      padding-bottom: 10px;
      position: relative;
      .time {
        font-size: 12px;
      }
      .content-text {
        padding-right: 5px;
        box-sizing: border-box;
        margin: 10px 0px;
      }
      .praise {
        position: absolute;
        top: 0px;
        right: 8px;
        .changePraise{
          color: red;
        }
      }
    }
  }
}
}


@keyframes rotate {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>